package com.spark.colliery.module.mine.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.content.LocalBroadcastManager
import android.util.ArrayMap
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.spark.colliery.MainActivity
import com.spark.colliery.R
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.server
import com.spark.common.ext.success
import com.spark.common.ext.wrapIO
import com.spark.common.helper.Picker
import com.spark.colliery.module.mine.activity.LoginActivity
import com.spark.colliery.module.mine.activity.ProfileActivity
import io.reactivex.Single
import kotlinx.android.synthetic.main.fragment_profile_main.*
import java.util.*

/**
 * 个人信息
 */
class ProfileMainFragment : BaseFragment() {
    companion object {
        fun newInstance(): ProfileMainFragment {
            val fragment = ProfileMainFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.fragment_profile_main
    }

    private val itemList = ArrayList<String>()

    private val childFragmentMap = ArrayMap<Int, Fragment>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        itemList.add("拍照")
        itemList.add("从相册选取")

        Const.getUser()?.apply {
            if (!user.pictureurl.isNullOrBlank())
                civ_avatar.server(user.pictureurl)
            tv_account.text = user.mobile
            tv_name.text = user.name
            tv_id.text = user.idcard
            tv_phone.text = user.mobile

        }

        tv_account.setOnClickListener { }
        iv_arrow_avatar.setOnClickListener {
            activity?.apply {
                Picker(this).show {}
            }
        }
        civ_avatar.setOnClickListener { v ->
            activity?.apply {
                Picker(this).show {}
            }
        }
        // 手机号码
        iv_arrow_phone.setOnClickListener { changePage(2) }
        // 密码
        iv_arrow_password_setting.setOnClickListener { changePage(3) }
        // 通知设置
        iv_arrow_notify_setting.setOnClickListener { changePage(4) }

        btn_logout.setOnClickListener { logout() }

        childFragmentMap[2] = ProfileChangePhoneFragment.newInstance()
        childFragmentMap[3] = ProfileChangePasswordFragment.newInstance()
        childFragmentMap[4] = ProfileNotifySettingFragment.newInstance()

        (activity as ProfileActivity).changeTitle("个人设置")
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        activity?.apply { Picker.onActivityResulted(this, requestCode, resultCode, data) }
        super.onActivityResult(requestCode, resultCode, data)
    }

    @SuppressLint("CheckResult")
    private fun logout() {
        Single.create<Boolean> { it.onSuccess(true) }
                .wrapIO()
                .subscribe({
                    if (it) {
                        success("成功退出")
                        activity?.apply {
                            ActivityUtils.finishActivity(MainActivity.javaClass)
                            finish()
                            LoginActivity.actionStart(this)
                            Const.delUser()
                            LocalBroadcastManager.getInstance(this).sendBroadcast(Intent(MainActivity.killselfAction))
                        }

                    } else {
                        error("退出失败")
                    }
                }, {})
    }

    private fun changePage(page: Int) {
        if (!childFragmentMap.keys.contains(page)) {
            return
        }
        val childFragment = childFragmentMap[page]
        activity!!.supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
                .replace(R.id.fl_content, childFragment)
                .addToBackStack(childFragment?.javaClass?.simpleName)
                .commit()
    }

    override fun onBackPressedSupport(): Boolean {
        LogUtils.e("child's count >>> ${childFragmentManager.backStackEntryCount}")
        return super.onBackPressedSupport()
    }
}
